package com.zj.utils.utils.utilcode;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * 获取本地MediaStore中的文件
 * @author jiang zhu on 2019/7/24
 */
public class LocalFileTool {
    public static final String[] picType = new String[]{"image/bmp", "image/jpeg", "image/png"};
    public static final String[] aviType = new String[]{"video/3gpp", "video/x-ms-asf", "video/x-msvideo",
            "video/vnd.mpegurl", "video/x-m4v", "video/quicktime", "video/mp4", "video/mpeg",
    };
    public static final String[] volumType = new String[]{"audio/x-mpegurl", "audio/mp4a-latm", "audio/x-mpeg", "audio/mpeg", "audio/ogg", "audio/x-wav", "audio/x-ms-wma"};
    public static final String[] docType = new String[]{"application/msword", "application/pdf", "application/vnd.ms-powerpoint", "text/plain", "application/vnd.ms-works"};
    public static final String[] zipType = new String[]{"application/zip"};

    // final static Object lock=new Object();

    @SuppressLint("CheckResult")
    public static void readFile(final String[] mimeType, Context context, final IReadCallBack iReadCallBack) {

        Observable.just(context).map(context1 -> {

            List<File> paths = new ArrayList<>();
            Uri[] fileUri = new Uri[]{MediaStore.Files.getContentUri("external")};
            String[] colums = new String[]{MediaStore.Files.FileColumns.DATA};
            //构造筛选语句
            StringBuilder selection = new StringBuilder();
            for (int i = 0; i < mimeType.length; i++) {
                if (i != 0) {
                    selection.append(" OR ");
                }
                selection.append(MediaStore.Files.FileColumns.MIME_TYPE).append(" LIKE '%").append(mimeType[i]).append("'");
            }

            //获取内容解析器对象
            ContentResolver resolver = context1.getContentResolver();
            //获取游标
            for (Uri uri : fileUri) {
                Cursor cursor = resolver.query(uri, colums, selection.toString(), null, null);
                if (cursor == null) {
                    return null;
                }//游标从最后开始往前递减，以此实现时间递减顺序（最近访问的文件，优先显示）
                long beginTime = System.currentTimeMillis();
                if (cursor.moveToLast()) {

                    do {
                        //输出文件的完整路径
                        String data = cursor.getString(0);
                        File file = new File(data);
                        paths.add(file);

                    } while (cursor.moveToPrevious());

                }
                cursor.close();
                Log.e("endTime", System.currentTimeMillis() - beginTime + "");
            }
            return paths;

        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).
                subscribe(iReadCallBack::callBack);

    }


    public interface IReadCallBack {
        void callBack(List<File> localPath);
    }


}
